package com.mutouren.web.org;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mutouren.common.entity.PageInfo;
import com.mutouren.common.entity.Result;
import com.mutouren.common.entity.ValidState;
import com.mutouren.common.utils.StringUtils;
import com.mutouren.common.web.BootStrapPage;
import com.mutouren.common.web.ParsePageInfo;
import com.mutouren.modules.org.model.App;
import com.mutouren.modules.org.service.AppService;
import com.mutouren.web.WebUtils;

@Controller
@RequestMapping(value="/org/app")
public class AppAction {

	@Resource
	private AppService appService;	
	
	@RequestMapping(value="/manage.action")
	public String manage(HttpServletRequest request) {
		PageInfo pageInfo = ParsePageInfo.parse(request);
		
		List<App> listApp = appService.select(null, pageInfo);
		request.setAttribute("listApp", listApp);
		
		request.setAttribute("mtrPage", BootStrapPage.buildHtmlPage(pageInfo));
		return "modules/org/appList";
	}
	
	@RequestMapping(value="/add.action")
	public String add(@ModelAttribute("app") App app, 
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));		
		if (!WebUtils.isPost(request)) {		
			return "modules/org/appForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, app, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}
		
		Result result = appService.register(app);
		if (result.getCode() == 0) {
			return WebUtils.successDialog(response);
		} else {
			return WebUtils.failDialog(response, "应用别名已存在，请重新命名");
		}
	}
	
	@RequestMapping(value="/modify.action")
	public String modify(
			@ModelAttribute("app") App app,
			@RequestParam(value="appId") int appId,
			ModelMap map,
			HttpServletRequest request, HttpServletResponse response) {
		
		request.setAttribute("actionName", WebUtils.getActionName(request));
		if (!WebUtils.isPost(request)) {
			app = appService.get(appId);
			map.addAttribute("app", app);
			return "modules/org/appForm";
		}
		
		List<String> errors = new ArrayList<String>();
		if(this.validate(request, app, errors)) {
			return WebUtils.failDialog(response, WebUtils.getErrorMessage(errors));
		}		
		
		int result = appService.modify(app);
		if (result == 1) {
			return WebUtils.successDialog(response);
		} else {
			return WebUtils.failDialog(response, "编辑数据不存在");
		}				
	}	
	
	@RequestMapping(value="/changeState.action")
	public void changeState(@RequestParam(value="appId") int appId, String validState,
			HttpServletResponse response) throws IOException {
		App paramApp = new App();
		paramApp.setAppId(appId);
		paramApp.setValidState(ValidState.valueOf(validState));
		
		if (appService.setState(paramApp) == 1) {
			response.getWriter().write("success");
		} else {
			response.getWriter().write("fail");
		}
	}
	
	@RequestMapping(value="/resetAppSecret.action")
	public void resetAppSecret(@RequestParam(value="appId") int appId, 
			HttpServletResponse response) throws IOException{
		
		appService.resetAppSecret(appId);
		response.getWriter().write("success");
	}
	
	@RequestMapping(value="/select.action")
	public String select(HttpServletRequest request) throws IOException{
		
		List<App> listApp = appService.select(null);
		request.setAttribute("listApp", listApp);

		return "modules/org/appSelect";
	}

	public boolean validate(HttpServletRequest request, Object model, List<String> errors ) {
		String actionName = (String) request.getAttribute("actionName");

		// app
		if (actionName.equals("add") || actionName.equals("modify")) {
			App register = (App) model;
			if (StringUtils.isBlank(register.getAppAlias())) {
				errors.add("应用别名: 不能为空");
			}
			if (StringUtils.isBlank(register.getAppName())) {
				errors.add("应用名称: 不能为空");
			}			
		}

		return errors.size() > 0;
	}
}
